﻿//by  张海军 710605420@qq.com
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LazyFramework;

//这是我弄的另外一个工程，包含了基本所有android的运行内存调用。@ https://gitee.com/winemike/Android-Memory-Leak.git

namespace LazyFramework
{
#if UNITY_ANDROID && !UNITY_EDITOR
    public class AndroidMemoryUsage : Singleton<AndroidMemoryUsage>
    {


        private AndroidJavaClass unityJc = null;
        private AndroidJavaObject unityContext = null;
        private AndroidJavaClass memoryUseageJc = null;

        public void Initialized()
        {
            unityJc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            unityContext = unityJc.GetStatic<AndroidJavaObject>("currentActivity");
            memoryUseageJc = new AndroidJavaClass("com.tencent.memoryuseLibrary.MemoryHelper");
            memoryUseageJc.CallStatic("Init", unityContext);
        }

        public float GetMemoryXmsMax()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetMemoryXmsMax");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }
        public float GetMemoryXmsFree()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetMemoryXmsFree");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }
        public float GetMemoryXmsTotalUsage()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetMemoryXmsTotalUsage");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }

        public float GetNativeMaxSize()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetNativeMaxSize");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }

        public float GetNativeFreeSize()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetNativeFreeSize");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }

        public float GetNativeAllocatedSize()
        {
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetNativeAllocatedSize");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }

        public float GetProcessPss()
        {
            //java 5s 计算一次
            if (memoryUseageJc != null)
            {
                return memoryUseageJc.CallStatic<float>("GetProcessPss");
            }
            else
            {
                Debug.LogError("sdk initial failed");
            }
            return 0f;
        }

    }
#endif
}
